package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:删除书籍
 * User: 86184
 * Date: 2023-02-18
 * Time: 15:18
 */
public class DelOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("请输入要删除的图书的书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = bookList.getUseSize();
        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getPos(i);
            if (name.equals(book.getName())) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            System.out.println("没有找到这本书！");
            return;
        } else {
            for (int i = index; i <currentSize-1 ; i++) {
                Book book=bookList.getPos(i+1);
                bookList.setBooks(book,i);
            }
        }
        bookList.setBooks(null,currentSize-1);

        bookList.setUseSize(currentSize-1);

        System.out.println("删除成功");
    }
}
